<?php
/**
 * @author fyl
 * Date: 16-3-29 下午2:59
 */

class AdminService extends BaseService
{
    private static $self        =   NULL;

    private $mStorage            =  NULL;
    private $mHelpCenter         =  NULL;
    private $mInformation        =  NULL;
    private $mProduct            =  NULL;
    private $mProductCate        =  NULL;
    private $mUser               =  NULL;
    private $mAdmin              =  NULL;
    private $mLog                =  NULL;
    private $mOrder              =  NULL;
    private $mAds                =  NULL;
    private $mInquiry            =  NULL;
    private $mCustomize          =  NULL;
    private $mSupply             =  NULL;
    private $ProductPic          =  NULL;
    private $mPurchaseResource  = Null;
    private $mNewResource        = NULL;
    private $mInviteCode        = NULL;
    private $mBrand            = NULL;
    private $mMaterial            = NULL;
    private $mFactory           = NULL;
    private $mOrderProduct           = NULL;
    private $mInvoice           = NULL;
    private $mCategorySpec           = NULL;
    private $mOrderPay           = NULL;
    private $mSupplier             =  NULL;
    private $mMessage             =  NULL;



    static public function instance()
    {
        if (self::$self == NULL) {
            self::$self = new self;
        }
        return self::$self;
    }


    public function __construct()
    {
        $this->mStorage             =   new Involved_StorageModel();
        $this->mHelpCenter          =   new Involved_HelpCenterModel();
        $this->mInformation         =   new Involved_InformationModel();
        $this->mProduct             =   new Involved_ProductModel();
        $this->mProductCate         =   new Involved_ProductCateModel();
        $this->mUser                =   new Involved_UserModel();
        $this->mAdmin               =   new Involved_AdminModel();
        $this->mLog                 =   new Involved_LogModel();
        $this->mOrder               =   new Involved_OrderModel();
        $this->mAds                 =   new Involved_AdsModel();
        $this->mInquiry             =   new Involved_InquiryModel();
        $this->mCustomize           =   new Involved_CustomizeModel();
        $this->mInviteCode          =   new Involved_InviteCodeModel();
        $this->mBrand               =   new Involved_BrandModel();
        $this->mMaterial            =   new Involved_MaterialModel();
        $this->mFactory             =   new Involved_FactoryModel();
        $this->mOrderProduct        =   new Involved_OrderProductModel();
        $this->mInvoice             =   new Involved_InvoiceModel();
        $this->mNewResource         =   new Involved_NewResourceModel();
        $this->mCategorySpec        =   new Involved_CategorySpecModel();
        $this->mOrderPay             =   new Involved_OrderPayModel();
        $this->mSupplier             =   new Involved_SupplierModel();
        $this->mMessage             =   new Involved_MessageModel();


    }


    /**
     * 验证用户
     */
    public function LoginValidator($user)
    {
        #此处只做简单验证
        if(empty($user->username) || empty($user->password)){
            throw new Exception('必填项不能为空', ProfessionErrorCodeEnum::STATUS_ERROR_PARAMS_MUST);
        }
        if(strtolower($user->captcha) != strtolower(Session::get('mt_Captcha'))){
            throw new Exception('验证码错误', ProfessionErrorCodeEnum::ERROR_VERIFICATION_CODE_WRONG);
        }

        #数据验证
        $admin_info = $this->mAdmin->getLoginAdminInfo($user->username,md5($user->password));
        if(!$admin_info){
            throw new Exception('密码错误', ProfessionErrorCodeEnum::ERROR_PASSWORD_WRONG);
        }
        #存储SESSION
        Session::put(UserEnum::ADMIN_USER_KEY_ID, $admin_info->id);
        Session::put(UserEnum::ADMIN_USER_KEY, $admin_info);
        return $admin_info;
    }


    /**
     * 資訊列表展示
     * @return mixed
     * @author fyl
     * @date 16-04-01
     */
    public function NewsList()
    {
        return  $this->mInformation->AdminInformationList();
    }

    /**
     * 咨询入庫
     * @param  $paramObj
     * @return int
     * @throws Exception
     * @author fyl
     * @date   2016-04-01
     */
    public function AddNews($paramObj)
    {
        #上传图片
        if(!empty($paramObj->thumb_img1))
            $paramObj->thumb_img1 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img1);
//        if(!empty($paramObj->thumb_img2))
//            $paramObj->thumb_img2 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img2);
//        if(!empty($paramObj->thumb_img3))
//            $paramObj->thumb_img3 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img3);

        #汇总数据
        $aData = [
            'title'              =>   $paramObj->title,
            'author'             =>   $paramObj->author,
            'source'             =>   $paramObj->source,
            'keywords'           =>   $paramObj->keywords,
            'thumb_img1'         =>   $paramObj->thumb_img1,
//            'thumb_img2'         =>   $paramObj->thumb_img2,
//            'thumb_img3'         =>   $paramObj->thumb_img3,
            'content'            =>   $paramObj->content,
            'cate'               =>   $paramObj->cate,
            'created'            =>   time(),
            'is_index'           =>   $paramObj->is_index,
        ];
        return $this->mInformation->insert($aData);
    }

    /**
     * 港口信息修改
     * @param $paramObj
     * @return bool
     * @author fyl
     */
    public function UpdateNews($paramObj)
    {
        #图片上传
        $paramObj->thumb_img1 = !empty($paramObj->thumb_img1) ?
            $paramObj->thumb_img1 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img1):$paramObj->thumb_img1_old;
//        $paramObj->thumb_img2 = !empty($paramObj->thumb_img2) ?
//            $paramObj->thumb_img2 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img2):$paramObj->thumb_img2_old;
//        $paramObj->thumb_img3 = !empty($paramObj->thumb_img3) ?
//            $paramObj->thumb_img3 =  QiniuUpload::instance()->upload_files($paramObj->thumb_img3):$paramObj->thumb_img3_old;

        #修改分类
        $aData = [
            'title'              =>   $paramObj->title,
            'author'             =>   $paramObj->author,
            'source'             =>   $paramObj->source,
            'keywords'           =>   $paramObj->keywords,
            'thumb_img1'         =>   $paramObj->thumb_img1,
//            'thumb_img2'         =>   $paramObj->thumb_img2,
//            'thumb_img3'         =>   $paramObj->thumb_img3,
            'content'            =>   $paramObj->content,
            'cate'               =>   $paramObj->cate,
            'is_index'           =>   $paramObj->is_index,
        ];
        return $this->mInformation->update($aData,['zx_id'=>$paramObj->zx_id]);
    }



    /**
     * 资讯单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneNews($paramObj)
    {
        $aData =  $paramObj->id;
        return $this->mInformation->GetNewsById($aData);
    }


    /**
     * 港口删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneNews($paramObj)
    {
        return $this->mInformation->delete(['zx_id'=>$paramObj->zx_id]);
    }





    /**
     *仓储服务
     * @throws Exception
     * @author lizhongpeng
     * @date   2016-03-030
     */
    public function ShowStorage()
    {
        return $this->mStorage->GetStorage();
    }
    /**
     * 仓储列表删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function StorageDelete($aData)
    {
        return $this->mStorage->delete(['storage_id' => $aData->id]);
    }

    /**
     *
     */
    public function GetStorageInfo($id)
    {
        return $this->mStorage->GetStorageInfo($id);
    }

    /**
     *仓储添加
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function StorageAdd($paramObj)
    {
//        #上传图片
//        if(!empty($paramObj->show_pic1))
//            $paramObj->show_pic1 =  QiniuUpload::instance()->upload_files($paramObj->show_pic1);
//        if(!empty($paramObj->show_pic2))
//            $paramObj->show_pic2 =  QiniuUpload::instance()->upload_files($paramObj->show_pic2);
//        if(!empty($paramObj->show_pic3))
//            $paramObj->show_pic3 =  QiniuUpload::instance()->upload_files($paramObj->show_pic3);

        #汇总数据
        $aData = [
            'storage_name'      => $paramObj->storage_name,
            'storage_address'   => $paramObj->storage_address,
            'storage_place'     => $paramObj->pro."-".$paramObj->city."-".$paramObj->coun,
            'storage_phone'         => $paramObj->storage_phone,
            'storage_work_time'         => $paramObj->storage_work_time,
            'storage_city'         => $paramObj->storage_city,

        ];
        return $this->mStorage->insert($aData);
    }

    /**
     *仓储修改
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function StorageUp($paramObj)
    {
        #修改分类
        $aData3 = [
            'storage_name'      => $paramObj->storage_name,
            'storage_address'   => $paramObj->storage_address,
            'storage_place'     => $paramObj->pro."-".$paramObj->city."-".$paramObj->coun,
            'storage_phone'         => $paramObj->storage_phone,
            'storage_work_time'         => $paramObj->storage_work_time,
            'storage_city'         => $paramObj->storage_city,
        ];

        return $this->mStorage->update($aData3,['storage_id'=>$paramObj->storage_id]);
    }

    /**
     * 钢厂列表
     *
     *
     */


    public function FactoryList(){
        return  $this->mFactory->FactoryList();

    }

    /**
     * @param $paramObj
     * @return int
     * 钢厂添加
     */
    public function FactoryAdd($paramObj)
    {
//        #上传图片
        if(!empty($paramObj->picture))
            $paramObj->pic =  QiniuUpload::instance()->upload_files($paramObj->picture);


        #汇总数据
        $aData = [
            'factory_name'      => $paramObj->factory_name,
            'contact_name'      => $paramObj->contact_name,
            'place'              => $paramObj->pro.'-'.$paramObj->city.'-'.$paramObj->coun,
            'address'            => $paramObj->address,
            'phone'              => $paramObj->phone,
            'email'              => $paramObj->email,
            'postcord'          => $paramObj->postcord,
            'remarks'           => $paramObj->remarks,
            'picture'           => $paramObj->pic,
            'recommend'           => $paramObj->recommend,

        ];
        return $this->mFactory->insert($aData);
    }

    public function GetFactoryInfo($id)
    {
        return $this->mFactory->GetFactoryInfo($id);
    }
    /**
     * 钢厂删除
     * @return mixed
     * @author
     * @date  2016-04-01
     */
    public function FactoryDelete($aData)
    {
        return $this->mFactory->delete(['factory_id' => $aData->id]);
    }
    public function ShowFactory()
    {
        return $this->mFactory->FactoryList();
    }
    public function FactoryUp($paramObj)
    {
        #图片上传
        $paramObj->picture = !empty($paramObj->picture) ?
            $paramObj->picture =  QiniuUpload::instance()->upload_files($paramObj->picture):$paramObj->picture_old;

        #修改分类
        $aData3 = [
            'factory_name'      => $paramObj->factory_name,
            'contact_name'      => $paramObj->contact_name,
            'place'              => $paramObj->pro.'-'.$paramObj->city.'-'.$paramObj->coun,
            'address'            => $paramObj->address,
            'phone'              => $paramObj->phone,
            'email'              => $paramObj->email,
            'postcord'          => $paramObj->postcord,
            'remarks'           => $paramObj->remarks,
            'picture'           => $paramObj->picture,
            'recommend'           => $paramObj->recommend,
        ];

        return $this->mFactory->update($aData3,['factory_id'=>$paramObj->factory_id]);
    }
    //收款记录添加
    public function OrderPayAdd($paramObj){
        if(!empty($paramObj->enclosure))
            $paramObj->pic =  QiniuUpload::instance()->upload_files($paramObj->enclosure);
        $aData = [
            'order_id'      => $paramObj->order_id,
            'amount'            => $paramObj->amount,
            'payment'              => $paramObj->payment,
            'bank_account'              => $paramObj->bank_account,
            'account'              => $paramObj->account,
            'account_user'              => $paramObj->account_user,
            'created'          => $paramObj->created,
            'message'           => $paramObj->message,
            'enclosure'           => $paramObj->pic,
            'pay_trade_num'      =>$paramObj->pay_trade_num,

        ];
        return $this->mOrderPay->insert($aData);

    }
    public function OrderPayDetails($order_id){
        return $this->mOrderPay->OrderPayDetails($order_id);

    }
    /**
     * 文章列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function ArticleList()
    {
        return $this->mHelpCenter->GetArticle();
    }
    /**
     * 文章删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function ArticleDelete($aData)
    {
        return $this->mHelpCenter->delete(['help_id' => $aData->id]);
    }
    /**
     * 文章添加
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function ArticleAdd($paramObj)
    {
        #汇总数据
        $aData = [
            'title'      =>  $paramObj->title,
            'content'    =>  $paramObj->content,
            'type'       =>  $paramObj->type,

        ];

        return $this->mHelpCenter->insert($aData);
    }
    /**
     * 文章修改
     * @return mixed
     * @author lzhp
     * @date  2016-04-01
     */
    public function ArticleUp($paramObj)
    {
        #修改分类
        $aData3 = [
            'title'      =>  $paramObj->title,
            'content'    =>  $paramObj->content,
            'type'       =>  $paramObj->type,
        ];
        return $this->mHelpCenter->update($aData3,['help_id'=>$paramObj->help_id]);
    }
    public function GetArticleId($id)
    {
        return $this->mHelpCenter->GetArticleId($id);
    }
    /**
     * 会员列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function ManagerUser()
    {
        return $this->mUser->fetchAll([],[],['user_id' => 'desc']);
    }

    /**
     * 会员下拉菜单
     * @return mixed
     * @author fyl
     * @date  2016-05-26
     */
    public function UserSelect()
    {
        return $this->mUser->UserSelect();
    }


    /**
     * 会员添加
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function CustomerAdd($paramObj)
    {
        if(empty($paramObj->password))
            return Redirect::to('/customer_import');
        if(!empty($paramObj->avatar))
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar);
        $aData = [
            'user_name'      => $paramObj->user_name,
            'mobile'         => $paramObj->mobile,
            'avatar'         => $paramObj->avatar,
            'password'       => md5($paramObj->password),
            'created'        => time()
        ];
        return $this->mUser->insert($aData);
    }

    /**
     * 会员删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function CustomerDelete($aData)
    {

        return $this->mUser->delete(['user_id'=>$aData->user_id]);
    }

    /**
     * 会员ajax返回
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function GetUserInfo($id)
    {
        return $this->mUser->GetUserInfo($id);
    }

    /**
     * 会员修改
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function CustomerUp($paramObj)
    {
        #图片上传
        $paramObj->avatar = !empty($paramObj->avatar) ?
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar):$paramObj->avatar_old;

        #修改分类
        $aData = [
            'user_name'      => $paramObj->user_name,
            'mobile'         => $paramObj->mobile,
            'avatar'         => $paramObj->avatar,
            'password'       => $paramObj->password
        ];

        return $this->mUser->update($aData,['user_id'=>$paramObj->user_id]);
    }

    /**
     * 管理员列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function ManageAdmin()
    {
        return $this->mAdmin->ManageAdmin();

    }

    /**
     * 管理员添加
     * @param $paramObj
     * @return int
     * @throws Exception
     */
    public function AdminAdd($paramObj)
    {
        if(empty($paramObj->password))
            return Redirect::to('/add_admin');
        if($paramObj->password!==$paramObj->sure)
            return Redirect::to('/add_admin');
        if(!empty($paramObj->avatar))
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar);
        $aData = [
            'name'           => $paramObj->name,
            'real_name'      => $paramObj->real_name,
            'mobile'         => $paramObj->mobile,
            'avatar'         => $paramObj->avatar,
            'password'       => md5($paramObj->password),
            'role_id'        => $paramObj->role_id,
            'status'         => $paramObj->status,
            'created'        => time()
        ];
        return $this->mAdmin->insert($aData);
    }

    /**
     * 管理员编辑
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function GetAdminInfo($id)
    {
        return $this->mAdmin->GetAdminInfo($id);
    }
    public function AdminUp($paramObj)
    {
        if($paramObj->password!==$paramObj->sure)
            return Redirect::to('/edit_admin');
        #图片上传
        $paramObj->avatar = !empty($paramObj->avatar) ?
            $paramObj->avatar =  QiniuUpload::instance()->upload_files($paramObj->avatar):$paramObj->avatar_old;

        #修改分类
        $aData = [
            'name'           => $paramObj->name,
            'real_name'      => $paramObj->real_name,
            'mobile'         => $paramObj->mobile,
            'avatar'         => $paramObj->avatar,
            'password'       => md5($paramObj->password),
            'role_id'        => $paramObj->role_id,
            'status'         => $paramObj->status,
            'created'        => time()
        ];
        return $this->mAdmin->update($aData,['id'=>$paramObj->id]);
    }

    /**
     * 管理员删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function DeleteAdmin($aData)
    {
        return $this->mAdmin->delete(['id'=>$aData->id]);
    }
    /**
     * 管理员角色列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function ManageAdminRole()
    {
        return $this->mRole->ManageAdminRole();
    }
    /**
     * 管理员角色添加
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function RoleAdd($paramObj)
    {
        $aData = [
            'role_name'        =>   $paramObj->role_name,
            'privilege'        =>   $paramObj->privilege
        ];

        return $this->mRole->insert($aData);
    }
    /**
     * 管理员角色编辑
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function GetRoleId($id)
    {

        return $this->mRole->GetRoleId($id);
    }

    /**
     *管理员角色修改
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function RoleUp($paramObj)
    {
        #修改分类
        $aData = [
            'role_name'        =>   $paramObj->role_name,
            'privilege'        =>   $paramObj->privilege
        ];

        return $this->mRole->update($aData,['role_id'=>$paramObj->role_id]);
    }
    /**
     * 管理员角色删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function DeleteAdminRole($paramObj)
    {
        return $this->mRole->delete(['role_id'=>$paramObj->role_id]);
    }


    /**
     * 日志列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function LogService()
    {
        return $this->mLog->LogService();
    }



    /**
     * 日志删除
     * @return mixed
     * @author lzhp
     * @date  2016-04-04
     */
    public function DeleteLog($aData)
    {
        return $this->mLog->delete(['log_id'=>$aData->log_id]);
    }

    /**
     * 商家列表展示
     * @@return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function ManageSupplier()
    {
        return $this->mSupplier->ManageSupplier();
    }

    /**
     * 商家列表展示
     * @@return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function SupplierInfo()
    {
        return $this->mSupplier->SupplierInfo();
    }

    /**
     * 添加商家
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function SupplierAdd($paramObj)
    {
        if(!empty($paramObj->operate_license))
            $paramObj->operate_license =  QiniuUpload::instance()->upload_files($paramObj->operate_license);
        if(!empty($paramObj->tax_license))
            $paramObj->tax_license =  QiniuUpload::instance()->upload_files($paramObj->tax_license);
        if(!empty($paramObj->show_img1))
            $paramObj->show_img1 =  QiniuUpload::instance()->upload_files($paramObj->show_img1);
        if(!empty($paramObj->show_img2))
            $paramObj->show_img2 =  QiniuUpload::instance()->upload_files($paramObj->show_img2);
        if(!empty($paramObj->show_img3))
            $paramObj->show_img3 =  QiniuUpload::instance()->upload_files($paramObj->show_img3);
        $aData = [
            'supplier_id'            =>     $paramObj->user_id,
            'supplier_name'          =>     $paramObj->supplier_name,
            'company'                =>     $paramObj->company,
            'address'                =>     $paramObj->address,
            'address_all'            =>     $paramObj->address_all,
            'phone'                  =>     $paramObj->phone,
            'operate_license'        =>     $paramObj->operate_license,
            'show_img1'              =>     $paramObj->show_img1,
            'show_img2'              =>     $paramObj->show_img2,
            'show_img3'              =>     $paramObj->show_img3,
            'created'                =>     time()

        ];
        $this->mUser->update(['is_supplier'=>1],['user_id'=>$paramObj->user_id]);
        return $this->mSupplier->insert($aData);
    }



    /**
     * 修改商家
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function SupplierUp($paramObj)
    {
        #图片上传
        $paramObj->operate_license = !empty($paramObj->operate_license) ?
            $paramObj->operate_license =  QiniuUpload::instance()->upload_files($paramObj->operate_license):$paramObj->operate_license_old;
        $paramObj->tax_license = !empty($paramObj->tax_license) ?
            $paramObj->tax_license =  QiniuUpload::instance()->upload_files($paramObj->tax_license):$paramObj->tax_license_old;
        $paramObj->show_img1 = !empty($paramObj->show_img1) ?
            $paramObj->show_img1 =  QiniuUpload::instance()->upload_files($paramObj->show_img1):$paramObj->show_img1_old;
        $paramObj->show_img2 = !empty($paramObj->show_img2) ?
            $paramObj->show_img2 =  QiniuUpload::instance()->upload_files($paramObj->show_img2):$paramObj->show_img2_old;
        $paramObj->show_img3 = !empty($paramObj->show_img3) ?
            $paramObj->show_img3 =  QiniuUpload::instance()->upload_files($paramObj->show_img3):$paramObj->show_img3_old;
        #修改分类
        $aData = [
            'supplier_name'          =>     $paramObj->supplier_name,
            'company'                =>     $paramObj->company,
            'owner'                  =>     $paramObj->owner,
            'address'                =>     $paramObj->address,
            'phone'                  =>     $paramObj->phone,
            'brand'                  =>     $paramObj->brand,
            'scale'                  =>     $paramObj->scale,
            'id_card'                =>     $paramObj->id_card,
            'bank_account'           =>     $paramObj->bank_account,
            'operate_license'        =>     $paramObj->operate_license,
            'tax_license'            =>     $paramObj->tax_license,
            'show_img1'              =>     $paramObj->show_img1,
            'show_img2'              =>     $paramObj->show_img2,
            'show_img3'              =>     $paramObj->show_img3,
            'market'                 =>     $paramObj->market,
            'feature'                =>     $paramObj->feature,
            'storage'                =>     $paramObj->storage,
            'business_scope'         =>     $paramObj->business_scope,
            'brief'                  =>     $paramObj->brief,
            'is_foreign'             =>     $paramObj->is_foreign,
            'created'                =>     time()
        ];

        return $this->mSupplier->update($aData,['supplier_id'=>$paramObj->supplier_id]);
    }
    /**
     * 删除替换
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function GeSupplierId($supplier_id)
    {
        return $this->mSupplier->GeSupplierId($supplier_id);
    }

    /**
     * 删除商家
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function DelSupplier($aData)
    {
        return $this->mSupplier->delete(['supplier_id'=>$aData->supplier_id]);
    }

    /**
     * 获取商家审核列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function SupplierExamineList()
    {
        return $this->mSupplier->GetSupplierExamine(ProductEnum::EXAMINE_ING);
    }

    /**
     * 获取商家审核列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function GetSupplierInfo($supplier_id)
    {
        return $this->mSupplier->GetSupplierInfo($supplier_id);
    }

    /**
     * 商家审核结果
     * @param $paramObj
     * @return int
     */
    public function SupplieUpdateStatus($paramObj)
    {
        #修改分类
        $aData = [
            'status'               =>      $paramObj->status,
            'examine'              =>      $paramObj->examine,
            'created'              =>      time(),
        ];
        if($aData['status']==1){
            $this->mUser->update(['is_supplier'=>1],['user_id'=>$paramObj->supplier_id]);
        }
        return $this->mSupplier->update($aData,['supplier_id'=>$paramObj->supplier_id]);
    }

    /**
     * 订单列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function OrderList()
    {
        return $this->mOrder->OrderList();
    }


    /**
     * 需开发票订单
     * @return mixed
     * @author fyl
     * @date  2016-08-23
     */
    public function GetInvoiceList()
    {
        return $this->mInvoice->GetInvoiceList();
    }

    /**
     * 需开发票订单入库
     * @author fyl
     * @date  2016-08-23
     */
    public function UpdateInvoiceAction($paramObj)
    {
        DB::beginTransaction();
        try {
            $aData = [
                'invoice_code' => $paramObj->invoice_code,
                'expressage_code' => $paramObj->expressage_code,
                'people' => session_get(UserEnum::ADMIN_USER_KEY)->real_name,
                'is_default'=>1,
                'created' => time(),
            ];
            $status = $this->mInvoice->update($aData,['invoice_id'=>$paramObj->invoice_id]);
            if($status){
                $this->mOrder->update(['order_status'=>OrderEnum::STATUS_SUCCESS],['order_id'=>$paramObj->order_id]);
            }
            #结束事务
            DB::commit();
            return $status;
        } catch(Exception $e){
            #事务回滚
            DB::rollback();
            throw $e;
        }

    }

    public function GetOrderDetail($order_id){
        return $this->mOrder->GetOrderDetailAdmin($order_id);
    }
    public function OrderUp($paramObj){
        return $this->mOrder->update(['order_status'=>$paramObj->order_status],['order_id'=>$paramObj->order_id]);
    }
    public function OrderLock($paramObj){
        $lock = $this->mOrderProduct->kcback($paramObj->order_id);
        if($paramObj->order_status == 20){
            $this->mProduct->lock($lock->number,$lock->product_id);
            $this->mProduct->lock1($lock->number,$lock->product_id);
        }else{
            $this->mProduct->cancellock($lock->number,$lock->product_id);
            $this->mProduct->cancellock1($lock->number,$lock->product_id);
        }
    }
    /**
     * 询价单列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function InquiryList()
    {
        return $this->mInquiry->InquiryList();
    }
    /**
     * 定制单列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function CustomizeList()
    {
        return $this->mCustomize->CustomizeList();
    }

    /**
     * 定制单详情
     * @return mixed
     * @author fyl
     * @date  2016-06-01
     */
    public function CustomizeOne($aData)
    {
        return $this->mCustomize->CustomizeOne($aData->customize_id);
    }

    /**
     * 修改定制单
     * @return mixed
     * @author fyl
     * @date  2016-06-01
     */
    public function CustomizeUpdate($aData)
    {
        $admin_id = Session::get(UserEnum::ADMIN_USER_KEY_ID);
        return $this->mCustomize->update(['status'=>$aData->status,'accept_peo'=>$admin_id],['customize_id'=>$aData->customize_id]);
    }

    /**
     * 供货单列表
     * @return mixed
     * @author lzhp
     * @date  2016-04-05
     */
    public function SupplyList()
    {
        return $this->mSupply->SupplyList();
    }


    /**---------------------------------产品------------------------------------*/

    /**
    产品列表
    */
    public function ShowProduct(){
        return $this->mProduct->AdminProductList();
    }


    /**
    产品审核列表
     */
    public function ProductExamineList(){
        return $this->mProduct->ProductExamineList();
    }


    /**
     *产品添加
     */
    public function AddProduct($paramObj)
    {
        #汇总数据
        $aData = [
            'product_brand'             => $paramObj->product_brand,
            'product_material'          => $paramObj->product_material,
            'product_spec'              => $paramObj->product_spec,
            'product_price'             =>$paramObj->product_price,
            'real_stock'                => $paramObj->real_stock,
            'useable_stock'             => $paramObj->real_stock,
            'piece_weight'              => $paramObj->piece_weight,
            'duty_people'               => $paramObj->duty_people,
            'storage'                   => $paramObj->storage,
            'factory'                   => $paramObj->factory,
            'add_people'                => session_get(UserEnum::ADMIN_USER_KEY_ID),
            'remarks'                   => $paramObj->remarks,
            'product_sn'                => ToolKit::make_product_sn(),
            'created'                   => time(),
            'category'                  => $paramObj->category,
        ];
        if($paramObj->product_id){
          return  $this->mProduct->update($aData,['product_id'=>$paramObj->product_id]);
        }else{
          return $this->mProduct->insert($aData);
        }

    }

    /**
     * 商品删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProduct($paramObj)
    {
        return $this->mProduct->delete(['product_id'=>$paramObj->product_id]);
    }



    /**
     * 产品单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProduct($paramObj)
    {
        $aData =  $paramObj->product_id;
        return $this->mProduct->AdminGetProductById($aData);
    }

    /**
     * 材质下拉菜单
     * @return int
     */
    public function SelectMaterial()
    {
      return $this->mMaterial->SelectMaterial();
    }

    /**
     * 品名下拉菜单
     * @return int
     */
    public function SelectBrand()
    {
        return $this->mBrand->SelectBrand();
    }

    /**
     * 品名条件下拉菜单
     * @return int
     */
    public function SelectBrandCategory($category)
    {
        return $this->mBrand->SelectBrandCategory($category);
    }



    /**
     * 仓库下拉菜单
     */
    public function SelectStorage()
    {
        return $this->mStorage->SelectStorage();
    }

    /**
     * 钢厂下拉菜单
     */
    public function SelectFactory()
    {
        return $this->mFactory->SelectFactory();
    }


    /**
     * 产品单条查询
     * @param $paramObj
     * @return int
     */
    public function UpdateStatus($paramObj)
    {
        #修改分类
        $aData = [
            'status'               =>      $paramObj->status,
        ];
        return $this->mProduct->update($aData,['product_id'=>$paramObj->product_id]);
    }


    /**
    产品品牌列表
     */
    public function ShowProductBrand(){
        return $this->mBrand->AdminProductBrandList();
    }
    public function SelectCategory($id){

        return $this->mCategorySpec->SelectCategory($id);
    }

    /**
     *品牌添加/修改
     */
    public function AddProductBrand($paramObj)
    {
        #汇总数据
        $aData = [
            'brand_name'        => $paramObj->brand_name,
            'cate_id'           =>$paramObj->cate_id,
        ];
        if($paramObj->brand_id){
            return $this->mBrand->update($aData,['brand_id'=>$paramObj->brand_id]);
        }else{
            return $this->mBrand->insert($aData);
        }

    }


    /**
     * 品牌单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProductBrand($paramObj)
    {
        $aData =  $paramObj->brand_id;
        return $this->mBrand->GetProductBrandById1($aData);
    }


    /**
     * 品牌删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProductBrand($paramObj)
    {
        $re = $this->mProduct->GetProductBrand($paramObj->brand_id);
        if($re){
            return  0;
        }else{
            return $this->mBrand->delete(['brand_id'=>$paramObj->brand_id]);
        }
    }

    /**
    商品材质列表
     */
    public function ShowProductMaterial(){
        return $this->mMaterial->AdminProductMaterialList();
    }

    /**
     * @param $m_name
     * @return mixed
     */
    public function ProductMaterialIs($m_name){
         $info = $this->mMaterial->ProductMaterialIs($m_name);
        if($info){
            $status = 1;//已存在
        }else{
            $status = 0;//不存在
        }
        return $status;
    }

    /**
     * @param $m_name
     * @return mixed
     */
    public function ProductBrandIs($brand_name){
        $info = $this->mBrand->ProductBrandIs($brand_name);
        if($info){
            $status = 1;//已存在
        }else{
            $status = 0;//不存在
        }
        return $status;
    }


    /**
     *材质添加/修改
     */
    public function AddProductMaterial($paramObj)
    {
        #汇总数据
        $aData = [
            'm_name'        => $paramObj->m_name,
            'm_remarks'        => $paramObj->m_remarks,
        ];
        if($paramObj->m_id){
            return $this->mMaterial->update($aData,['m_id'=>$paramObj->m_id]);
        }else{
            return $this->mMaterial->insert($aData);
        }

    }


    /**
     * 材质单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProductMaterial($paramObj)
    {
        $aData =  $paramObj->m_id;
        return $this->mMaterial->GetProductMaterialById1($aData);
    }


    /**
     * 材质删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProductMaterial($paramObj)
    {
        $re = $this->mProduct->GetProductMaterial($paramObj->m_id);
        if($re){
            return  0;
        }else{
            return $this->mMaterial->delete(['m_id'=>$paramObj->m_id]);
        }
    }


    /**
    产品分类列表
     */
    public function ShowProductCate(){
        return $this->mProductCate->AdminProductCateList();
    }

    /**
    产品全部分类列表
     */
    public function ShowProductCateAll(){
        return $this->mProductCate->AdminProductCateAllList();


    }

    /**
   缓存分类列表
     */
    public function CacheCate(){
            #获取分类
            $Category = $this->mProductCate->GetProductCategory();
            $category = [];
            foreach ($Category as $cate) {
                #获取品类
                $classes = $this->mProductCate->GetProductClasses($cate->cate_id);
                $classes_list = [];
                foreach ($classes as $v){
                    #获取材质
                    $quality = $this->mProductCate->GetProductQuality($v->cate_id);
                    $quality_list = [];
                    foreach ($quality as $q) {
                           #获取规格
                        //  $spec = $this->mSpec->GetSpecList($q->cate_id);

                          #获取等级
                        //  $level = $this->mLevel->GetLevelList($q->cate_id);
                        $quality_list[] = [
                            'id'    => $q->cate_id,
                            'name'  => $q->cate_name,
                            /*  'spec'  => $spec,
                              'level' => $level,*/
                        ];
                    }
                    $classes_list[] = [
                        'id'         => $v->cate_id,
                        'name'       => $v->cate_name,
                        'quality'    => $quality_list
                    ];
                }

                $category[] = [
                    'id' => $cate->cate_id,
                    'name' => $cate->cate_name,
                    'classes' => $classes_list
                ];
            }
        Cache::forever('cache_cate', $category);
        return 1;
    }



    /**
     * 分类单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProductCate($paramObj)
    {
        $aData =  $paramObj->cate_id;
        return $this->mProductCate->GetProductCateById($aData);
    }
    public function UpdateProductCate($paramObj){
        $cate_id=$paramObj->cate_id;
        $aData =  [
            'cate_name'          =>     $paramObj->cate_name,
            'parent_id'          =>     $paramObj->parent_id,
            'length'             =>     $paramObj->length,
            //'remark '            => 0 ,
        ];

        return $this->mProductCate->UpdateProductCate($cate_id,$aData);

    }

    /**
     * 分类删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProductCate($paramObj)
    {
        $re = $this->mProductCate->GetProductCateParent($paramObj->cate_id);
        if($re){
            return  0;
        }else{
            return $this->mProductCate->delete(['cate_id'=>$paramObj->cate_id]);
        }
    }


    /**
     产品材质列表
     */
    public function ShowProductCateQuality(){
        return $this->mProductCate->AdminProductCateQualityList();
    }

    /**
    产品品类列表
     */
    public function ShowProductCateClasses(){
        return $this->mProductCate->AdminProductCateClassesList();
    }



    /**
    产品规格入库
     */
    public function AddProductSpec($paramObj)
    {
        #添加分类
        $aData = [
            'spec_name'          =>     $paramObj->spec_name,
            'quality'             =>     $paramObj->quality,
            'sort_by'             =>     $paramObj->sort_by,
        ];
        //dd($aData);
        return $this->mSpec->insert($aData);
    }


    /**
    产品材质入库
     */
    public function AddProductCate($paramObj)
    {
        $aData = [
            'cate_name'           =>     $paramObj->cate_name,
            'parent_id'           =>     $paramObj->parent_id,
            'rank'                =>     $paramObj->rank,
            'length'              =>     $paramObj->length,
        ];
        #添加分类
        if($paramObj->cate_id){
           return $this->mProductCate->update($aData,['cate_id'=>$paramObj->cate_id]);
        }else{
            return $this->mProductCate->insert($aData);
        }

    }



    /**
     * 广告位列表
     */
    public function AdsList()
    {
        return $this->mAds->AdsList();
    }

    /**
     * 广告位添加
     * @param $paramObj
     * @return int
     */
    public function AdsAdd($paramObj)
    {
        if(!empty($paramObj->img))
            $paramObj->img =  QiniuUpload::instance()->upload_files($paramObj->img);
        #添加
        $aData = [
            'ads_name'          =>     $paramObj->ads_name,
            'img'               =>     $paramObj->img,
            'type'              =>     $paramObj->type,
            'is_rec'            =>     $paramObj->is_rec,
            'order_by'          =>     $paramObj->order_by,
            'link'              =>     $paramObj->link,
            'created'           =>     time(),
        ];
        //dd($aData);
        return $this->mAds->insert($aData);
    }
    public function AdsUp($paramObj)
    {
        #图片上传

        $paramObj->img = !empty($paramObj->img) ?
            $paramObj->img =  QiniuUpload::instance()->upload_files($paramObj->img):$paramObj->img_old;
        #修改
        $aData = [
            'ads_name'          =>     $paramObj->ads_name,
            'img'               =>     $paramObj->img,
            'type'              =>     $paramObj->type,
            'is_rec'            =>     $paramObj->is_rec,
            'order_by'          =>     $paramObj->order_by,
            'link'              =>     $paramObj->link,
        ];

        return $this->mAds->update($aData,['ads_id'=>$paramObj->ads_id]);

    }
    public function GetAdsInfo($ads_id)
    {
        return $this->mAds->GetAdsInfo($ads_id);
    }
    public function DelAds($paramObj)
    {
        return $this->mAds->delete(['ads_id'=>$paramObj->ads_id]);
    }
    public function AdsAjax()
    {
        return $this->mAds->AdsList();
    }



    /**
     *现货列表
     */
    public function NewResource(){
        return $this->mNewResource->AdminNewResourceList();

    }

    /**
     * 删除现货
     * @return mixed
     * @author fyl
     * @date  2016-04-05
     */
    public function DeleteNewResource($aData)
    {
        return $this->mNewResource->delete(['product_id'=>$aData->product_id]);
    }

    /**
     *现货详情
     */
    public function NewResourceInfo($aData){
        $info = $this->mNewResource->NewResourceDetails($aData);
        $img  = $this->mProductPic->DetailsPic($aData);
        $info->show_img1 = $img->show_img1;
        $info->show_img2 = $img->show_img2;
        $info->show_img3 = $img->show_img3;
        return $info;

    }


    /**
     * 现货审核结果
     * @param $paramObj
     * @return int
     */
    public function NewResourceUpdateStatus($paramObj)
    {
        #修改分类
        $aData = [
            'status'               =>      $paramObj->status,
            //'examine'              =>      $paramObj->examine,
            'examine_time'              =>      time(),
        ];
        if($paramObj->status==1){
            $user_id = $this->mNewResource->NewResourceDetails($paramObj)->user_id;
            ShopService::instance()->AddMessage('资源单审核','您上传的资源单已经通过审核。',$user_id,2);
        }
        if($paramObj->status==0){
            $user_id = $this->mNewResource->NewResourceDetails($paramObj)->user_id;
            ShopService::instance()->AddMessage('资源单审核','您上传的资源单未通过审核。',$user_id,2);
        }

        return $this->mNewResource->update($aData,['product_id'=>$paramObj->product_id]);
    }

    /**
     * 获取邀请码
     */
    public function GetInviteCode() {
        list($s1, $s2) = explode(' ', microtime());
        return substr(sprintf('%.0f', (floatval($s1) * floatval($s2)) * 10000),0,12);
    }



    /**
     * 获取邀请码
     */
    public function AddInviteCode() {

        for($i=0;$i<=200;$i++){
            $invite_code['invite_code'] = $this->GetInviteCode();
             $this->mInviteCode->insert($invite_code);
        }
        return 1;
    }

    public function JsCheckCaptcha($paramObj)
    {
        if(strtolower($paramObj->captcha) == strtolower(Session::get('mt_Captcha'))){
            return 1;
        }else{
            return 2;
        }
    }

    /**
     * @param $paramObj
     * @return mixed
     * 电话查询
     */
        public function SelectUserMobile($paramObj){
            return $this->mUser->SelectUserMobile($paramObj->key_word);
        }

    /**
     * 订单入库
     */
    public function DownOrderImportAction($paramObj){
        #获取购物车信息
        $product_info  =  $this->mProduct->GetGoodsInfo($paramObj->product_id);
        #开始事务
        DB::beginTransaction();
        try{
            $order_sn       =   ToolKit::make_order_sn();
            $order_data = [
                'payment_id'         => $paramObj->pay,                          //支付方式
                'user_id'            => $paramObj->user_id,                      //用户ID
                'order_time'         => time(),                                  //订单生成时间
                'order_sn'           => $order_sn,                               //订单号
                'confirm_time'       => time(),                                  //确认订单时间
                'order_status'       => OrderEnum::STATUS_NO_LOCKING,                //订单状态
                'amount'             => $product_info->product_price*$paramObj->number*$product_info->piece_weight,    //订单总价
                'trade_num'          => 'GLW'.time().rand(100,999),                   //交易流水号
            ];
            $order_id               =   $this->mOrder->insert($order_data);
            if(!$order_id)
                throw new Exception('订单创建失败！', ErrorCodeEnum::STATUS_ERROR);
            $aData = [
                'product_id'    =>  $paramObj->product_id,
                'order_id'      =>  $order_id,
                'number'        =>   empty($paramObj->number)?'':$paramObj->number,
            ];
            $order_product = $this->mOrderProduct->insert($aData);
            #结束事务
            DB::commit();
            return $order_id;
        } catch(Exception $e){
            #事务回滚
            DB::rollback();
            throw $e;
        }
    }

    /**
     * @param $id
     * @return mixed
     * 规格查询
     */
    public function GetSpec($id){
        $spec = $this->mCategorySpec->GetSpec($id);
        $info = [
            'spec_list'=>$spec,
            'cate_id'=>$id,
        ];
        return $info;
    }
    /**
     *
     * 品类查询
     */
    public function GetCategory(){
        return $this->mCategorySpec->GetCategory();
    }

    /**
     * @param $brand_id
     * @return mixed
     * 规格查询
     */
    public function SelectSpec($brand_id){
        $cate_id = $this->mBrand->GetProductBrandById1($brand_id)->cate_id;
        $spec = $this->GetSpec($cate_id);
       return $spec;
    }
    /**
     *
     * 品类管理
     */
    public function ShowProductCategory(){
        return $this->mCategorySpec->ShowProductCategory();
    }

    /**
     * @param $paramObj
     * @return bool|int
     * 品类添加
     */
    public function AddProductCategory($paramObj)
    {
        #汇总数据
        $aData = [
            'category_name'        => $paramObj->category_name,
            'fid'                    => $paramObj->fid,
        ];
        if($paramObj->category_id){
            return $this->mCategorySpec->update($aData,['id'=>$paramObj->category_id]);
        }else{
            return $this->mCategorySpec->insert($aData);
        }

    }
    /**
     * 品牌删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProductCategory($paramObj)
    {
            return $this->mCategorySpec->delete(['id'=>$paramObj->category_id]);
    }
    /**
     * 品类单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProductCategory($paramObj)
    {
        $aData =  $paramObj->category_id;
        return $this->mCategorySpec->GetProductCategoryName($aData);
    }

    /**
     *
     * 规格管理
     */
    public function ShowProductSpec(){
        return $this->mCategorySpec->ShowProductSpec();
    }

    /**
     * 规格单条查询
     * @param $paramObj
     * @return int
     */
    public function SelectOneProductSpec($paramObj)
    {
        $aData =  $paramObj->spec_id;
        return $this->mCategorySpec->GetProductSpecName($aData);
    }

    /**
     * 规格删除
     * @param $paramObj
     * @return int
     */
    public function DeleteOneProductSpec($paramObj)
    {
        return $this->mCategorySpec->delete(['id'=>$paramObj->spec_id]);
        $spec = $this->GetSpec($cate_id);
       return $spec;
        $spec = $this->GetSpec($cate_id);
       return $spec;
    }


    /**
     * 通知列表
     * @return int
     */
    public function MessageList()
    {
        return $this->mMessage->MessageList();
    }



    /**
     * 系统通知入库
     * @param $paramObj
     * @return int
     */
    public function MessageAddAction($paramObj)
    {
        #汇总数据
        $aData = [
            'title'        => $paramObj->title,
            'content'                    => $paramObj->content,
            'status'       => 2,
            'created'      => time(),
        ];
        $user_id = $this->mUser->GetAllUserId();
        foreach($user_id as $v){
            $aData['user_id'] = $v->user_id;
            $this->mMessage->insert($aData);
        }
        return true;
    }



}